<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        :class="{ active: sorter === 'weight_desc' }"
        @click="changeSorter('weight_desc')"
        >推荐</a
      >
      <a
        href="javascript:;"
        :class="{ active: sorter === '' }"
        @click="changeSorter('')"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in rows"
        :key="item.id"
        :item="item"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import { getArticles } from '../api/article'
export default {
  name: 'article-page',
  data() {
    return {
      rows: [],
      loading: false,
      finished: false,
      current: 1,
      sorter: 'weight_desc'
    }
  },
  methods: {
    async onLoad() {
      // 分页加载思路:
      // 1.需要用到 van-list 组件，提供了 loading 状态和 load 事件
      // 2.当 list 滚动到底部时，会触发 load 事件 并将 loading 设置成 true
      // 3.底部会出现一个 加载... 字样，可以发送请求了（在 load 事件中）
      // 4.请求完毕后 将 loading 改为 false
      // 5.每次请求要将 current 加一
      // 6.请求到的数据不能直接覆盖，而是要追加到数组末尾
      const res = await getArticles({
        current: this.current,
        pageSize: 10,
        sorter: this.sorter
      })
      // console.log(res)
      // this.rows = res.data.data.rows
      this.rows.push(...res.data.data.rows)

      // 将 loading 设置为 false
      this.loading = false

      // 防止过度操作导致的其它 BUG
      if (this.current >= res.data.data.pageTotal) {
        this.finished = true
      }

      // 加载完毕后 让 current 自增
      this.current++
    },
    changeSorter(sorter) {
      this.sorter = sorter

      // 重置数据
      this.rows = []
      this.current = 1
      this.finished = false

      // 手动加载更多时 将 loading 设置为 true
      this.loading = true
      this.onLoad()
    }
  },
  async created() {
    // const res = await getArticles({
    //   current: 1,
    //   pageSize: 10
    // })
    // // console.log(res)
    // this.rows = res.data.data.rows
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
